﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assets.Scripts.network.socket.message
{
  public abstract  class AbstractJsonGameMessage<T> : AbstractGameMessage
    {
        private T bodyObj = Activator.CreateInstance<T>();


        public T GetBodyObj()
        {
            return bodyObj;
        }

        public override byte[] Write()
        {
            if(bodyObj != null)
            {
                string value = JsonUtil.JsonToString(bodyObj);
                return StringUtil.stringToBytes(value);
            }
            return null;

        }

        public override void Read(byte[] body)
        {
            if(body != null)
            {
                String value = StringUtil.bytesToString(body);
                bodyObj = JsonUtil.JsonToObject<T>(value);
            }

        }
        public override abstract int GetMessageID();
        public override abstract int GetServiceID();
        public override abstract EnumMessageType GetMessageType();
        public override abstract string Log();

    }
}
